﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class MissionDetailRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<MissionDetail> GetAllData()
        {
            return from MissionDetail in db.MissionDetails
                   orderby MissionDetail.MissionDetailID descending
                   select MissionDetail;
        }

        public IQueryable<MissionDetail> GetAllDataByMissionID(Int64 MissionID)
        {
            return from MissionDetail in db.MissionDetails 
                   where MissionDetail.MissionID == MissionID
                   orderby MissionDetail.MissionDetailID descending
                   select MissionDetail;
        }

        public IQueryable<MissionDetail> GetAllDataByMissionType(string missiontype)
        {
            if (missiontype == "Outbound")
                return from MissionDetail in db.MissionDetails
                       where MissionDetail.DepartureDate.HasValue == false && (MissionDetail.DriverReturnDate.HasValue == false || MissionDetail.VehicleReturnDate.HasValue == false)
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "Inbound")
                return from MissionDetail in db.MissionDetails
                       where MissionDetail.DepartureDate.HasValue == true && (MissionDetail.DriverReturnDate.HasValue == false || MissionDetail.VehicleReturnDate.HasValue == false)
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "MissionReturned")
                return from MissionDetail in db.MissionDetails
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue ==  true && MissionDetail.VehicleReturnDate.HasValue == true && (MissionDetail.VehicleQCPassDate.HasValue == false || MissionDetail.VehicleQCRejectedDate.HasValue == false)
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "History")
                return from MissionDetail in db.MissionDetails
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue == true && MissionDetail.VehicleReturnDate.HasValue == true && MissionDetail.VehicleQCPassDate.HasValue == true
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "QCPassRegister")
                return from MissionDetail in db.MissionDetails
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue == true && MissionDetail.VehicleReturnDate.HasValue == true && MissionDetail.VehicleQCPassDate.HasValue == false
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "QCRejectedRegister")
                return from MissionDetail in db.MissionDetails
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue == true && MissionDetail.VehicleReturnDate.HasValue == true && MissionDetail.VehicleQCPassDate.HasValue == false && MissionDetail.VehicleQCRejectedDate.HasValue == false
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else
                return from MissionDetail in db.MissionDetails
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
        }


        public IQueryable<MissionDetail> GetDataByName(string searchField, string searchOper, string searchString, Int64 MissionID)
        {
            var ps = db.MissionDetails.Where(p => p.MissionDetailID.ToString().Contains(searchString) && p.MissionID == MissionID);
            if (searchField == "MissionDetailID")
                ps = db.MissionDetails.Where(p => p.MissionDetailID.ToString().Contains(searchString) && p.MissionID == MissionID);
            if (searchField == "MissionID")
                ps = db.MissionDetails.Where(p => p.MissionID.ToString().Contains(searchString) && p.MissionID == MissionID);
            if (searchField == "TMR")
                ps = db.MissionDetails.Where(p => p.MissionHeader.TMR.Contains(searchString) && p.MissionID == MissionID);
            if (searchField == "VehicleNumber")
                ps = db.MissionDetails.Where(p => p.VehicleNumber.Contains(searchString) && p.MissionID == MissionID);
            if (searchField == "LicensePlateNumber")
                ps = db.MissionDetails.Where(p => p.LicensePlateNumber.Contains(searchString) && p.MissionID == MissionID);
            if (searchField == "DriverName")
                ps = db.MissionDetails.Where(p => p.DriverName.Contains(searchString) && p.MissionID == MissionID);

            return ps.AsQueryable();
        }

        public IQueryable<MissionDetail> GetDataByMissionType(string searchField, string searchOper, string searchString, string missiontype)
        {
            var ps = db.MissionDetails.Where(p => p.MissionDetailID.ToString().Contains(searchString));
            if (searchField == "MissionDetailID")
                ps = db.MissionDetails.Where(p => p.MissionDetailID.ToString().Contains(searchString));
            if (searchField == "MissionID")
                ps = db.MissionDetails.Where(p => p.MissionID.ToString().Contains(searchString));
            if (searchField == "TMR")
                ps = db.MissionDetails.Where(p => p.MissionHeader.TMR.Contains(searchString));
            if (searchField == "VehicleNumber")
                ps = db.MissionDetails.Where(p => p.VehicleNumber.Contains(searchString));
            if (searchField == "LicensePlateNumber")
                ps = db.MissionDetails.Where(p => p.LicensePlateNumber.Contains(searchString));
            if (searchField == "DriverName")
                ps = db.MissionDetails.Where(p => p.DriverName.Contains(searchString));

            //return ps.AsQueryable();
            if(missiontype == "Outbound")
                return from MissionDetail in ps
                       where MissionDetail.DepartureDate.HasValue == false && (MissionDetail.DriverReturnDate.HasValue == false || MissionDetail.VehicleReturnDate.HasValue == false)
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "Inbound")
                return from MissionDetail in ps
                       where MissionDetail.DepartureDate.HasValue == true && (MissionDetail.DriverReturnDate.HasValue == false || MissionDetail.VehicleReturnDate.HasValue == false)
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "MissionReturned")
                return from MissionDetail in ps
                       where MissionDetail.DepartureDate.HasValue == true && (MissionDetail.DriverReturnDate.HasValue == false || MissionDetail.VehicleReturnDate.HasValue == false) && (MissionDetail.VehicleQCPassDate.HasValue == false || MissionDetail.VehicleQCRejectedDate.HasValue == false)
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;                
            else if (missiontype == "History")
                return from MissionDetail in ps
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue == true && MissionDetail.VehicleReturnDate.HasValue == true && MissionDetail.VehicleQCPassDate.HasValue == true
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "QCPassRegister")
                return from MissionDetail in ps
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue == true && MissionDetail.VehicleReturnDate.HasValue == true && MissionDetail.VehicleQCPassDate.HasValue == false 
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else if (missiontype == "QCRejectedRegister")
                return from MissionDetail in ps
                       where MissionDetail.DepartureDate.HasValue == true && MissionDetail.DriverReturnDate.HasValue == true && MissionDetail.VehicleReturnDate.HasValue == true && MissionDetail.VehicleQCPassDate.HasValue == false && MissionDetail.VehicleQCRejectedDate.HasValue == false 
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
            else
                return from MissionDetail in ps
                       orderby MissionDetail.MissionDetailID descending
                       select MissionDetail;
        }

        public IQueryable<MissionDetail> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString, Int64 MissionID)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString, MissionID).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString, MissionID).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString, MissionID).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllDataByMissionID(MissionID).OrderByDescending(sortColumn);
                    else
                        return GetAllDataByMissionID(MissionID).OrderBy(sortColumn);
                else
                    return GetAllDataByMissionID(MissionID);
            }
        }

        public IQueryable<MissionDetail> GetAllDataByMissionType(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString, string missiontype)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByMissionType(searchField, searchOper, searchString, missiontype).OrderByDescending(sortColumn);
                    else
                        return GetDataByMissionType(searchField, searchOper, searchString, missiontype).OrderBy(sortColumn);
                else
                    return GetDataByMissionType(searchField, searchOper, searchString, missiontype).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllDataByMissionType(missiontype).OrderByDescending(sortColumn);
                    else
                        return GetAllDataByMissionType(missiontype).OrderBy(sortColumn);
                else
                    return GetAllDataByMissionType(missiontype);
            }
        }



        public MissionDetail GetByID(long id)
        {
            return db.MissionDetails.SingleOrDefault(MissionDetail => MissionDetail.MissionDetailID == id);
        }

        //Inset
        public void Add(MissionDetail MissionDetail)
        {
            //if (MissionDetail.StatusID == 0 )
            //    MissionDetail.StatusID = 1;
            MissionDetail.InsertedUser = HttpContext.Current.User.Identity.Name;
            MissionDetail.InsertedDate = DateTime.Now;
            MissionDetail.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionDetail.UpdatedDate = DateTime.Now;
            MissionDetail.DataVersion = 1;

            db.MissionDetails.AddObject(MissionDetail);
        }

        //Update
        public void Update(MissionDetail MissionDetail)
        {

            MissionDetail originalMissionDetail = GetByID(MissionDetail.MissionDetailID);

            MissionDetail.InsertedUser = originalMissionDetail.InsertedUser;
            MissionDetail.InsertedDate = originalMissionDetail.InsertedDate;
            MissionDetail.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionDetail.UpdatedDate = DateTime.Now;
            MissionDetail.DataVersion = ++originalMissionDetail.DataVersion;
  
            db.MissionDetails.ApplyCurrentValues(MissionDetail);
        }

        //Delete
        public void Delete(MissionDetail MissionDetail)
        {
            db.MissionDetails.DeleteObject(MissionDetail);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}